package initialize

import (
	"context"
	"github.com/cy77cc/hioshop/global"
	"github.com/google/uuid"
	"time"
)

// 令牌桶限流
func CreateCron() {
	// 生成UUID
	newUUID, _ := uuid.NewUUID()
	//fmt.Println("test")
	lLen := global.REDIS.LLen(context.Background(), "limit_list")
	// 桶里最多防两百个令牌
	if lLen.Val() >= 5000 {
		return
	}
	cmd := global.REDIS.RPush(context.Background(), "limit_list", newUUID.String())
	if cmd.Err() != nil {
		return
	}
}

func GenToken() {
	//c := cron.New()
	//spec := "*/1 * * * * *"
	//_ = c.AddFunc("@every 0.001s", CreateCron)
	//c.Start()
	tick := time.Tick(time.Millisecond * 10)
	for {
		select {
		case <-tick:
			CreateCron()
		}
	}
}
